import {
  getJctj,
} from '../../../../api/statistics'
import * as echarts from '../../../../components/ec-canvas/echarts';
let chart = null;
var option = {
  tooltip: {
    trigger: 'item'
  },
  legend: {},
  grid: {
    top: '7%',
    left: '2%',
    right: '4%',
    bottom: '2%',
    containLabel: true
  },
  xAxis: {
    type: 'value',
    boundaryGap: [0, 0.01]
  },
  yAxis: {
    type: 'category',
    data: []
  },
  series: [{
    name: '企业数量',
    type: 'bar',
    data: []
  }, {
    name: '检查次数',
    type: 'bar',
    data: []
  }]
};

function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);
  return chart;
}
Component({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    startDate: '',
    endDate: '',
    ec: {
      onInit: initChart
    },
    form: {
      hy: '',
      zwbj: '',
      qh: '',
      startDate: '',
      endDate: ''
    }
  },
  methods: {
    // 时间选择器返回数据
    dateChange(e) {
      this.setData({
        'form.startDate': e.detail.startDate,
        'form.endDate': e.detail.endDate
      })
    },
    // 区选择器返回数据

    quChange(event) {
      this.setData({
        'form.qh': event.detail.bm,
      })
    },
    // 主委办局返回数据
    zwbjChange(event) {
      this.setData({
        'form.zwbj': event.detail.bm
      })
    },
    // 行业返回数据
    sshyChange(event) {
      this.setData({
        'form.hy': event.detail
      })
    },
    clear() {
      this.setData({
        form: {
          hy: '',
          zwbj: '',
          qh: '',
          startDate: '',
          endDate: ''
        }
      })
      this.initData()
    },
    initData() {
      this.setData({
        loading: true
      })
      getJctj({
        startdate: this.data.form.startDate,
        enddate: this.data.form.endDate,
        hy: this.data.form.hy,
      }).then(res => {
        let seriesData = []
        let yAxisData = []
        let jcnum = []
        let qynum = []
        res.data.forEach(item => {
          seriesData.push({
            name: item.mch,
            value: Number(item.qynum),
            bm: item.bm
          })
          yAxisData.push(item.mch);
          jcnum.push(item.jcnum);
          qynum.push(item.qynum);
        })
        option.series[0].data = qynum;
        option.series[1].data = jcnum;
        option.yAxis.data = yAxisData
        // 给chart图表添加接口返回的数据
        setTimeout(() => {
          chart.setOption(option);
          this.setData({
            loading: false
          })
        }, 2000);
      })
    },
  },
  ready() {
    this.initData()
  },
})